# 阿里云SSH连接设置脚本
# 快速配置SSH连接

param(
    [string]$IP,
    [string]$User = "root",
    [int]$Port = 22,
    [string]$KeyPath,
    [string]$HostName = "aliyun-ecs"
)

Write-Host "🔧 设置阿里云SSH连接..." -ForegroundColor Cyan

# 检查IP是否提供
if (-not $IP) {
    Write-Host ""
    Write-Host "📋 请提供以下信息来配置SSH连接" -ForegroundColor Yellow
    Write-Host ""
    $IP = Read-Host "请输入公网IP地址"
    $User = Read-Host "请输入用户名 (默认: root)"
    if ([string]::IsNullOrEmpty($User)) { $User = "root" }
    
    $useKey = Read-Host "是否使用密钥文件? (y/n)"
    if ($useKey -eq 'y') {
        $KeyPath = Read-Host "请输入密钥文件路径 (如: C:\Users\xxx\.ssh\key.pem)"
    }
    
    Write-Host ""
}

# 验证IP格式
if (-not ($IP -match '^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')) {
    Write-Host "❌ IP地址格式不正确" -ForegroundColor Red
    return
}

# 确保.ssh目录存在
$sshDir = "$env:USERPROFILE\.ssh"
if (!(Test-Path $sshDir)) {
    New-Item -Path $sshDir -ItemType Directory | Out-Null
    Write-Host "✅ 创建.ssh目录" -ForegroundColor Green
}

# 处理密钥文件
$identityFile = ""
if ($KeyPath) {
    if (Test-Path $KeyPath) {
        # 复制密钥到.ssh目录
        $keyName = Split-Path $KeyPath -Leaf
        $targetKey = Join-Path $sshDir $keyName
        
        Copy-Item $KeyPath $targetKey -Force
        Write-Host "✅ 复制密钥文件到 .ssh 目录" -ForegroundColor Green
        
        # 设置密钥文件权限
        icacls $targetKey /inheritance:r 2>$null | Out-Null
        icacls $targetKey /grant:r "$($env:USERNAME):(R)" 2>$null | Out-Null
        
        $identityFile = $targetKey
        Write-Host "✅ 设置密钥文件权限" -ForegroundColor Green
    } else {
        Write-Host "⚠️  密钥文件不存在，将在配置中使用指定路径" -ForegroundColor Yellow
        $identityFile = $KeyPath
    }
}

# 创建或更新SSH配置
$configPath = Join-Path $sshDir "config"
$configExists = Test-Path $configPath

if ($configExists) {
    Write-Host "📝 更新SSH配置文件..." -ForegroundColor Yellow
} else {
    Write-Host "📝 创建SSH配置文件..." -ForegroundColor Yellow
}

# 生成配置内容
$configContent = @"

# 阿里云ECS - $HostName
Host $HostName
    HostName $IP
    Port $Port
    User $User
    StrictHostKeyChecking no
"@

if ($identityFile) {
    $configContent += "`n    IdentityFile $identityFile"
}

# 添加到配置文件
if ($configExists) {
    # 检查是否已存在该主机配置
    $configText = Get-Content $configPath -Raw
    if ($configText -notmatch "Host $HostName`b") {
        Add-Content -Path $configPath -Value "`n$configContent"
        Write-Host "✅ 配置已添加" -ForegroundColor Green
    } else {
        Write-Host "⚠️  配置已存在，跳过添加" -ForegroundColor Yellow
    }
} else {
    Set-Content -Path $configPath -Value $configContent
    Write-Host "✅ 配置文件已创建" -ForegroundColor Green
}

# 显示配置信息
Write-Host ""
Write-Host "=" * 60 -ForegroundColor Cyan
Write-Host "✅ SSH配置完成！" -ForegroundColor Green
Write-Host "=" * 60 -ForegroundColor Cyan
Write-Host ""
Write-Host "📋 配置信息:" -ForegroundColor Yellow
Write-Host "   Host: $HostName" -ForegroundColor White
Write-Host "   IP: $IP" -ForegroundColor White
Write-Host "   端口: $Port" -ForegroundColor White
Write-Host "   用户: $User" -ForegroundColor White
if ($identityFile) {
    Write-Host "   密钥: $identityFile" -ForegroundColor White
}
Write-Host ""
Write-Host "💻 使用方法:" -ForegroundColor Yellow
Write-Host "   ssh $HostName" -ForegroundColor Cyan
Write-Host ""
Write-Host "📝 或直接连接:" -ForegroundColor Yellow
if ($identityFile) {
    Write-Host "   ssh -i `"$identityFile`" $User@$IP -p $Port" -ForegroundColor Cyan
} else {
    Write-Host "   ssh $User@$IP -p $Port" -ForegroundColor Cyan
}
Write-Host ""

